import * as THREE from 'three';
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';


import baked from "./resource/image/baked.jpg"
import ground from "./resource/image/ground.jpg"
import room from "./resource/gltf/room.glb"

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000);
camera.position.set(200, 100, 200); //设置相机位置


const size = 120;
const divisions = 40;
const gridHelper = new THREE.GridHelper(size, divisions);
scene.add(gridHelper);


var point = new THREE.PointLight(0xffffff);
//设置点光源的位置
point.position.set(200, 100, 300);
//将点光源添加到场景中
scene.add(point);



const texture = new THREE.TextureLoader().load(
    baked
)
texture.flipY = false
texture.encoding = THREE.sRGBEncoding

const loader = new GLTFLoader();
loader.load(room, function (gltf) {
    gltf.scene.traverse(child => {
        if (child.isMesh) {
            if (child.name == 'ground') {
                const groundTexture = new THREE.TextureLoader().load(ground)
                groundTexture.flipY = false
                child.material = new THREE.MeshBasicMaterial({
                    map: groundTexture
                })
            } else {
                child.material = new THREE.MeshBasicMaterial({
                    map: texture
                })
            }
        }
    })

    scene.add(gltf.scene);
});


const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xa57f89, 1);

renderer.outputEncoding = THREE.sRGBEncoding

document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);


(function rendererCanvas() {
    requestAnimationFrame(rendererCanvas);
    renderer.render(scene, camera);
})();